// routes/index.js

/**
 * @swagger
 * tags:
 *   name: Folder
 *   description: 文件夹管理接口
 */

/**
 * @swagger
 * tags:
 *   name: File
 *   description: 文件管理接口
 */

const Router = require("koa-router");
const uploadController = require("../controllers/uploadController");
const router = new Router({ prefix: '/api' });

// ------------------------ 文件夹操作 ------------------------

/**
 * @swagger
 * /api/folder/create:
 *   post:
 *     summary: 创建文件夹
 *     tags: [Folder]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               folderName:
 *                 type: string
 *                 example: "我的文档"
 *               parentId:
 *                 type: integer
 *                 nullable: true
 *                 example: 5
 *     responses:
 *       200:
 *         description: 创建成功
 *       400:
 *         description: 参数错误或重名
 *       401:
 *         description: 未登录
 */
router.post('/folder/create', uploadController.createFolder);

/**
 * @swagger
 * /api/folder/list:
 *   get:
 *     summary: 获取文件夹列表（按父级）
 *     tags: [Folder]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: parentId
 *         schema:
 *           type: integer
 *           nullable: true
 *         description: 父文件夹 ID，null 为根目录
 *     responses:
 *       200:
 *         description: 返回文件夹数组
 */
router.get('/folder/list', uploadController.getFolderList);

/**
 * @swagger
 * /api/folder/delete:
 *   post:
 *     summary: 删除文件夹（级联软删除）
 *     tags: [Folder]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *                 example: 10
 *     responses:
 *       200:
 *         description: 删除成功
 *       404:
 *         description: 文件夹不存在
 */
router.post('/folder/delete', uploadController.deleteFolder);

/**
 * @swagger
 * /api/folder/rename:
 *   post:
 *     summary: 重命名文件夹
 *     tags: [Folder]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *                 example: 8
 *               newName:
 *                 type: string
 *                 example: "新名称"
 *     responses:
 *       200:
 *         description: 重命名成功
 */
router.post('/folder/rename', uploadController.renameFolder);

/**
 * @swagger
 * /api/folder/move:
 *   post:
 *     summary: 移动文件夹
 *     tags: [Folder]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *                 example: 7
 *               newParentId:
 *                 type: integer
 *                 nullable: true
 *                 example: 3
 *     responses:
 *       200:
 *         description: 移动成功
 */
router.post('/folder/move', uploadController.moveFolder);

// ------------------------ 文件操作 ------------------------

/**
 * @swagger
 * /api/file/upload:
 *   post:
 *     summary: 上传文件
 *     tags: [File]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               file:
 *                 type: array
 *                 items:
 *                   type: string
 *                   format: binary
 *               folderId:
 *                 type: integer
 *                 nullable: true
 *     responses:
 *       200:
 *         description: 上传成功，返回文件信息
 */
router.post('/file/upload', uploadController.uploadFile);

/**
 * @swagger
 * /api/file/list:
 *   get:
 *     summary: 分页查询文件列表
 *     tags: [File]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: folderId
 *         schema:
 *           type: integer
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *       - in: query
 *         name: sortBy
 *         schema:
 *           type: string
 *           enum: [created_at, name, size, updated_at]
 *       - in: query
 *         name: sortOrder
 *         schema:
 *           type: string
 *           enum: [ASC, DESC]
 *     responses:
 *       200:
 *         description: 返回分页文件列表
 */
router.get('/file/list', uploadController.getFileList);

/**
 * @swagger
 * /api/file/delete:
 *   post:
 *     summary: 删除文件（逻辑删除）
 *     tags: [File]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 example: [1,2,3]
 *     responses:
 *       200:
 *         description: 删除成功
 */
router.post('/file/delete', uploadController.deleteFile);

/**
 * @swagger
 * /api/file/rename:
 *   post:
 *     summary: 重命名文件
 *     tags: [File]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *                 example: 5
 *               newName:
 *                 type: string
 *                 example: "新文件名.txt"
 *     responses:
 *       200:
 *         description: 重命名成功
 */
router.post('/file/rename', uploadController.renameFile);

/**
 * @swagger
 * /api/file/move:
 *   post:
 *     summary: 移动文件到指定文件夹
 *     tags: [File]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: integer
 *               targetFolderId:
 *                 type: integer
 *                 nullable: true
 *     responses:
 *       200:
 *         description: 移动成功
 */
router.post('/file/move', uploadController.moveFile);

/**
 * @swagger
 * /api/file/download:
 *   get:
 *     summary: 下载文件
 *     tags: [File]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: id
 *         schema:
 *           type: integer
 *         required: true
 *     responses:
 *       200:
 *         description: 返回文件流
 *       404:
 *         description: 文件不存在
 */
router.get('/file/download', uploadController.downloadFile);

module.exports = router;